package services;

import controllers.SystemViewController;
import controllers.ViewManager;
import database.dao.Impl.CommodityDaoImpl;
import database.dao.Impl.InvoiceDaoImpl;
import database.dao.Impl.OrderRecordDaoImpl;
import database.dao.OrderRecordDao;
import entity.*;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

import java.util.HashMap;
import java.util.List;

/**
 * @ClassName SystemContext
 * @Description TODO
 * @Author lzq
 * @Date 2021/6/18 9:07
 */

public class SystemContext {
    private static SystemContext instance=null;
    private SystemContext(){}
    public static SystemContext getInstance(){
        if(instance==null){
            instance=new SystemContext();
        }
        return instance;
    }


    private User user;
    private String id;
    private ObservableList<Commodity>commodityObservableList;
    private Boolean isCustomer=true;
    private HashMap<String,Commodity>buyList=new HashMap<>();
    private ObservableList<UserOrder>userOrderObservableList;
    private ObservableList<BusinessOrder>businessOrderObservableList;
    private ObservableList<InvoiceRecord> invoiceRecordObservableList;

    public ObservableList<InvoiceRecord> getInvoiceRecordObservableList() {
        return invoiceRecordObservableList;
    }

    public void setInvoiceRecordObservableList(ObservableList<InvoiceRecord> invoiceRecordObservableList) {
        this.invoiceRecordObservableList = invoiceRecordObservableList;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public ObservableList<Commodity> getCommodityObservableList() {
        return commodityObservableList;
    }

    public ObservableList<UserOrder> getUserOrderObservableList() {
        return userOrderObservableList;
    }

    public ObservableList<BusinessOrder> getBusinessOrderObservableList() {
        return businessOrderObservableList;
    }

    public void setBusinessOrderObservableList(ObservableList<BusinessOrder> businessOrderObservableList) {
        for (BusinessOrder value:businessOrderObservableList) {
            System.out.println(value.getOrder_id()+"  "+value.getUserName());
        }
        this.businessOrderObservableList = businessOrderObservableList;
    }

    public void setUserOrderObservableList(ObservableList<UserOrder> userOrderObservableList) {
        for (UserOrder value : userOrderObservableList) {
            System.out.println(value.getOrder_id() + value.getOrder_date());
        }
        this.userOrderObservableList = userOrderObservableList;
    }

    public void refreshInvoiceList() {
        InvoiceDaoImpl invoiceDao = new InvoiceDaoImpl();
        List<InvoiceRecord> invoiceRecords = invoiceDao.SelectInvoice(id);
        setInvoiceRecordObservableList(FXCollections.observableList(invoiceRecords));
        SystemViewController controller = (SystemViewController) (ViewManager.CONTROLLER.get("SystemViewController"));

        if (controller.OrderTableView != null) {
            controller.OtherTable.setItems(invoiceRecordObservableList);
            controller.OtherTable.refresh();
        }
    }

    public void refreshOrderList(){
        if(SystemContext.getInstance().getIsCustomer()) {
            OrderRecordDao orderRecordDao = new OrderRecordDaoImpl();
            List<UserOrder> userOrderList = null;
            userOrderList = orderRecordDao.selectOrderRecord(SystemContext.getInstance().getId());
            SystemContext.getInstance().setUserOrderObservableList(FXCollections.observableList(userOrderList));
        }else{
            OrderRecordDao orderRecordDao = new OrderRecordDaoImpl();
            List<BusinessOrder> businessOrderList = null;
            businessOrderList = orderRecordDao.business_selectOrderRecord(SystemContext.getInstance().getId());
            SystemContext.getInstance().setBusinessOrderObservableList(FXCollections.observableList(businessOrderList));
        }

        SystemViewController controller=(SystemViewController)(ViewManager.CONTROLLER.get("SystemViewController"));

        if(controller.OrderTableView!=null) {
            if(SystemContext.getInstance().getIsCustomer()) {
                System.out.println("userOrderObservableList");
                controller.OrderTableView.setItems(userOrderObservableList);
            } else {
                System.out.println("businessOrderObservableList");
                controller.OrderTableView.setItems(businessOrderObservableList);
            }
        }

    }

    public void refreshCommodityList(){

        CommodityDaoImpl commodityDao=new CommodityDaoImpl();
        List<Commodity> commodityList=null;

        if(!isCustomer)
            commodityList=commodityDao.SelectCommodity(SystemContext.getInstance().getId());
        else
            commodityList=commodityDao.SelectCommodity();

        SystemContext.getInstance().setCommodityObservableList(FXCollections.observableList(commodityList));


        SystemViewController controller=(SystemViewController)(ViewManager.CONTROLLER.get("SystemViewController"));
        if(controller.CommodityList!=null){
            Platform.runLater(new Runnable() {
                @Override
                public void run() {
                    controller.CommodityList.setItems(commodityObservableList);
                }
            });
        }
    }

    public void setCommodityObservableList(ObservableList<Commodity> commodityObservableList) {
        this.commodityObservableList = commodityObservableList;
        for(Commodity value:commodityObservableList){
            System.out.println(value.getName());
        }
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Boolean getIsCustomer() {
        return isCustomer;
    }

    public void setIsCustomer(Boolean customer) {
        isCustomer = customer;
    }

    public void removeBuyList(String commodity_id) {
        buyList.remove(commodity_id);
    }

    public void addBuyList(Commodity commodity) {
        buyList.put(commodity.getCommodity_id().toString(),commodity);
    }

    public HashMap<String, Commodity> getBuyList() {
        return buyList;
    }
}
